<?php
/**
 * This code by Matt McNamara
 * mcnamamj@notes.udayton.edu
 * Created for Facebook, 2007
 * 
 * Modded by Bramus! - http://www.bram.us/ 
 *
 */

define('PICS_PER_ALBUM', 200); // number of pics a facebook album can hold
define('PICS_PER_FLICKRPAGE', 500); // the number of pics flickr returns per page of an album (albums are returned in subsets (named pages)!)

function createFacebookAlbum($client, $flickrAlbum, $curAlbumNum, $numAlbums)
{
	$albumName = $flickrAlbum . ' ('. $curAlbumNum . '/' . $numAlbums . ')';

	echo "Attempting to create new album: $albumName\n";
	flush();
	$result = $client->photos_createAlbum($albumName, null , $albumName);
	flush();
	echo "* ";
	if(isset($user_profile['error_response']) && isset($user_profile['error_response']['error_msg']) && ($result['error_response']['error_msg'] != ""))
	{
		echo "<!-- Error below: \n";
		print_r($result['error_response']);
		echo "\n-->\n";
		die("A fatal error from Facebook: <b>" . $result['error_response']['error_msg'] . "</b>");	
	}
	echo "Successfully created.\n";
	return $result['photos_createAlbum_response']['aid'];
}

if(!isset($_GET['x']))
{
	die("Unset x...");
}
if(!isset($_GET['a']))
{
	die("Unset a...");
}
if(!isset($_GET['aid']))
{
	$flickrAlbumNameid = null; //
} else {
	$flickrAlbumNameid = (int) $_GET['aid'];
}
if(!isset($_GET['from']))
{
	$from = 0; //
} else {
	$from = (int) $_GET['from'] - 1;
}
	if(!isset($_GET['pageid']))
	{
		$pageId = 1;
	} else {
		$pageId = (int) $_GET['pageid'];
	}

if (!function_exists('htmlspecialchars_decode'))
{
function htmlspecialchars_decode($text) {
	return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
}
}

include_once 'facebook_conf.php';
include_once '../library/facebook/facebookapi_php4_restlib.php';

//if they are not logged in, send them to the login page.
if (empty($_COOKIE['facebook_session_key']) || empty($_COOKIE['facebook_uid'])) {
	$cookiesSet = array_keys($_COOKIE);
	for ($flickrAlbumId=0;$flickrAlbumId<count($cookiesSet);$flickrAlbumId++) setcookie($cookiesSet[$flickrAlbumId],"",time()-1);
	//they can't login if they are still logged into facebook, so
	print "Login failed.";
	die();
}
echo "Upload code based on http://developer.yahoo.com/php/samples/bbauth/uploadphotoFSOCK.txt\n";
flush();
$session_key = $_COOKIE['facebook_session_key'];
$uid = $_COOKIE['facebook_uid'];
$client = new FacebookRestClient($config['rest_server_addr'], $config['api_key'], $config['secret'], $session_key, false, true);
$user_profile = $client->users_getInfo($uid, "name");
if(isset($user_profile['error_response']) && isset($user_profile['error_response']['error_msg']) && ($user_profile['error_response']['error_msg'] != ""))
{
		echo "<!-- Error below: \n";
		print_r($user_profile['error_response']);
		echo "\n-->\n";
		die("A fatal error from Facebook: <b>" . $user_profile['error_response']['error_msg'] . "</b>");
}
$name = $user_profile['users_getInfo_response']['user']['name'];
$flickrAlbumId = strip_tags($_GET['x']);
$flickrAlbumName = strip_tags($_GET['a']);
echo "<pre>Attempting to pull photos for photoset " . $flickrAlbumId . " and place them into $flickrAlbumName 's Facebook album... \n";
flush();

require_once("../library/phpFlickr/phpFlickr.php");
$f = new phpFlickr($config['flickr'], null, true);
$photos = $f->photosets_getPhotos($flickrAlbumId, null, null, null, $pageId);
$count = count($photos['photo'])-1;

echo "Found " . $count . " pics in the album; Starting at " . $from . " and looping up to " . ($count + (($pageId-1) * PICS_PER_FLICKRPAGE) + 1) . "\n";

$numAlbums = ceil($count / PICS_PER_ALBUM);

for($i = $from; $i < ($count + (($pageId-1) * PICS_PER_FLICKRPAGE) + 1);++$i)
{
	if (($flickrAlbumNameid == null) || (($i % PICS_PER_ALBUM) == 0))
	{
		$flickrAlbumNameid = createFacebookAlbum($client, $flickrAlbumName, (ceil($i / PICS_PER_ALBUM) + 1), $numAlbums);
	}
	
	echo "Working on picture " . ($i + 1) . "\n";
	$photoInfo = $photos['photo'][$i - ((($pageId-1) * PICS_PER_FLICKRPAGE) + 0)];
	$photoURL = "http://farm" . $photoInfo['farm'] . ".static.flickr.com/" . $photoInfo['server'] . "/" . $photoInfo['id'] . "_" . $photoInfo['secret'] . "_" . $config['photoSize'] . ".jpg";
	echo "<img src=\"$photoURL\">\n";
	flush();
	$photoStream = file_get_contents($photoURL);
	
	$result = $client->photos_upload($flickrAlbumNameid, $photoInfo['title'], $photoStream);
	echo "Returned from restlib, results below:\n";
	print_r($result);
	flush();
	
	echo "\n";
}
?>
<script>
parent.done();
</script>
</pre>